﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Net;
using System.Net.Mail;

public partial class Admin_LoaiSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HienThiDanhSach();

    }

    void HienThiDanhSach()
    {
        
        // get all LoaiSach
        DataTable Manufacturers = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllLoaiSach");
        GridView1.DataSource = Manufacturers;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
        btninsert.Enabled = false;
    }
    void HienThiKetQuaSua(String maloai)
    {
        
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_maloaisach",maloai)
        };

        // get LoaiSach By MaLoaiSach
        DataTable dtloai = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getLoaiSachByMa", thamso);

        lbma.Text = dtloai.Rows[0]["MaLoaiSach"].ToString();
        txttenloai.Text = dtloai.Rows[0]["TenLoaiSach"].ToString();

        MultiView1.ActiveViewIndex = 1;
    }

    protected void btntaomoi_Click(object sender, EventArgs e)
    {
        btnupdate.Visible = false;
        btninsert.Visible = true;
        MultiView1.ActiveViewIndex = 1;
        btninsert.Enabled = true;
        txttenloai.Text = "";
        lbma.Text = "Mã Loại sẽ được gán tăng lên!!!";

    }


    protected void btntmkiem_Click(object sender, EventArgs e)
    {
        string tenloai = txttim.Text;
        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_tenloai",tenloai)
        };

        // Search LoaiSach -- get LoaiSach By TenLoai
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchLoaiSachByTenLoai", thamso);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        txttim.Text = "";
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.HienThiDanhSach();

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnupdate.Visible = true;
        btninsert.Visible = false;
        string maloai = GridView1.SelectedRow.Cells[2].Text;
        this.HienThiKetQuaSua(maloai);
    }
    protected void btnback_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        int maloai = int.Parse(lbma.Text);
        string tenloai = txttenloai.Text;
       
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_tenloai",tenloai),
            new OracleParameter("p_maloai",maloai),
        };

        // update LoaiSach(TenLoai) by MaLoaiSach 
        DataConnection.GetDatatableByStoreProcedureName("update_LoaiSachByMaLoaiSach", thamso);
        this.HienThiDanhSach();
    }


    protected void btninsert_Click(object sender, EventArgs e)
    {
        int ma = DataConnection.TruyVanMax("SELECT MAX(MALOAISACH) FROM LOAISACH");
        string tenloai = txttenloai.Text;
        
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_maloaisach",ma),
            new OracleParameter("p_tenloaisach",tenloai)    
        };

        // insert new loaisach
        DataConnection.getPara("insertLoaiSach", thamso);
        this.HienThiDanhSach();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string maloai = GridView1.Rows[e.RowIndex].Cells[2].Text;
      
        OracleParameter[] thamSo = new OracleParameter[]{
            new OracleParameter("p_maloaisach",maloai)
        };

        // delete LoaiSach By MaLoaiSach
        DataConnection.GetDatatableByStoreProcedureName("delete_LoaiSachByMaLoaiSach", thamSo);
        HienThiDanhSach();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
}